package com.ktgj.customer.controller;

import java.lang.management.LockInfo;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.utils.helper.StringHelper;
import com.ktgj.customer.domain.Employee;
import com.ktgj.customer.service.ILoginService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.EmployeeVipresource;
import com.ktgj.customer.service.IEmployeeVipresourceService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-09-20
 */
@RestController
@RequestMapping("/vipresource")
public class EmployeeVipresourceController extends BaseController
{
    @Autowired
    private IEmployeeVipresourceService employeeVipresourceService;
    @Resource
    private ILoginService loginService;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize(hasPermi = "customer:vipresource:list")
    @PostMapping("/list")
    public TableDataInfo list(HttpServletRequest request, @RequestBody EmployeeVipresource employeeVipresource) throws Exception
    {
        startPage();
        Employee employee = loginService.getLoginEmployee(request);
        if (employee != null)
        {
            employeeVipresource.setEmployeeId(employee.getEmployeeId());
            List<EmployeeVipresource> list = employeeVipresourceService.selectEmployeeVipresourceList(employeeVipresource);
            return getDataTable(list);
        } else
        {
            throw new Exception("登录员工已过期");
        }
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize(hasPermi = "customer:vipresource:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmployeeVipresource employeeVipresource) throws IOException
    {
        List<EmployeeVipresource> list = employeeVipresourceService.selectEmployeeVipresourceList(employeeVipresource);
        ExcelUtil<EmployeeVipresource> util = new ExcelUtil<EmployeeVipresource>(EmployeeVipresource.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize(hasPermi = "customer:vipresource:query")
    @GetMapping(value = "/{vipresourceId}")
    public AjaxResult getInfo(@PathVariable("vipresourceId") Long vipresourceId)
    {
        return AjaxResult.success(employeeVipresourceService.selectEmployeeVipresourceById(vipresourceId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "customer:vipresource:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmployeeVipresource employeeVipresource)
    {
        return toAjax(employeeVipresourceService.insertEmployeeVipresource(employeeVipresource));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "customer:vipresource:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmployeeVipresource employeeVipresource)
    {
        return toAjax(employeeVipresourceService.updateEmployeeVipresource(employeeVipresource));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "customer:vipresource:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{vipresourceIds}")
    public AjaxResult remove(@PathVariable Long[] vipresourceIds)
    {
        return toAjax(employeeVipresourceService.deleteEmployeeVipresourceByIds(vipresourceIds));
    }

    //回收VIP资源
    @PostMapping("/returnVipResource")
    public AjaxResult returnVipResource(@RequestBody Map<String,Object> map)
    {
        Long employeeId = Long.valueOf(map.get("employeeId").toString());
        if (employeeId == null)
        {
            return AjaxResult.error("参数不能为空！");
        }
        String phone = map.get("phone").toString();
        int result = 0;
        if (employeeId != null && !StringHelper.isNullOrEmpty(phone))
        {
            result = employeeVipresourceService.deleteVipResourceBySingle(employeeId, phone);
        } else
        {
            result = employeeVipresourceService.deleteVipResourceByEmployeeId(employeeId);
        }
        if (result > 0)
        {
            return AjaxResult.success("回收资源成功！");
        }
        return AjaxResult.error("回收资源失败！");
    }
}
